<?php
include_once('admin_global.php');
$grid=new bn_grid();
$post=new bn_post();
$theme->add_block('body');

$grid->name='post';
$grid->delete->enable=true;
$grid->delete->caption=$lang[17];
$grid->delete->cssclass='button red';
$grid->update->enable=true;
$grid->update->caption=$lang[16];
$grid->update->cssclass='button blue';
$grid->header->enable=true;
$grid->paging->enable=true;
$grid->paging->caption=$lang[75];
$grid->paging->cssclass='button';

function per_date($date)
{
    if(isset($date)&& $date!='0000-00-00 00:00:00')
    return bndate('Y/n/d',$date);
    else
    return '';
}

function strip_text($text)
{
    $text=strip_tags($text);
    if(strlen($text)>150)
    return substr($text,0,150);
    else
    return $text;
}

$cid=new column;
$cid->field_name='id';
$cid->hide=true;
$grid->columns->append($cid);

$ctitle=new column;
$ctitle->caption=$lang[67];
$ctitle->template='
<label name="label" id="label"><%title%></label>
<br />
<span name="text" id="text"><%strip_text(text)%></span>';
$grid->columns->append($ctitle);

$ccat=new column;
$ccat->caption=$lang[13];
$ccat->template='<label name="cat" id="cat" data-cat="<%cat_id%>"><%cat%></label>';
$grid->columns->append($ccat);

$cuser=new column;
$cuser->caption=$lang[1];
$cuser->field_name='user';
$grid->columns->append($cuser);

$cdate=new column;
$cdate->caption=$lang[23];
$cdate->field_name='date';
$cdate->function='per_date';
$grid->columns->append($cdate);

$cuseru=new column;
$cuseru->caption=$lang[68];
$cuseru->field_name='modif_user';
$grid->columns->append($cuseru);

$cdateu=new column;
$cdateu->caption=$lang[69];
$cdateu->field_name='modif_date';
$cdateu->function='per_date';
$grid->columns->append($cdateu);

$cmnt=new column;
$cmnt->caption=$lang[70];
$cmnt->field_name='comment';
$cmnt->template='<input type="submit" name="comment" id="comment" value="<%comment%>" class="button"/>';
$grid->columns->append($cmnt);

$cstate=new column;
$cstate->caption=$lang[59];
$cstate->field_name='status';
$cstate->template='<input type="submit" name="state" id="state" value="<%status%>" class="button"/>';
$grid->columns->append($cstate);

$cmd=$_POST['cmd'];
if(isset($cmd))
{
    extract($_POST);
       if($cmd=='state')
    {
        if($state=='disable')
        $state='enable';
        else
        $state='disable';
        $post->update_col('status',$state,$id);
    }
    
    if($cmd=='comment')
    {
        if($state=='disable')
        $state='enable';
        else
        $state='disable';
        $post->update_col('comment',$state,$id);
    }
    
    if($cmd=='delete')
    $post->delete($id);
    
    if($cmd=='update')
    bn_redirect_postdata('post.php',array('cmd'=>'update','id'=>$post_id));
   
   die($grid->show($post->select_allposts()->orderby('id desc')->fetch(),$post_page));
}

$theme->add_script('
$("#comment").live("click",function(){
    var id=$(this).closest("tr").find("td :eq(0)").html();
    $.post(window.location.href,{cmd:"comment",state:$(this).val(),id:id,page:$("#post_page").val()},function(data){
        $("#show").html(data);     
    });
});

$("#state").live("click",function(){
    var id=$(this).closest("tr").find("td :eq(0)").html();
    $.post(window.location.href,{cmd:"state",state:$(this).val(),id:id,page:$("#post_page").val()},function(data){
        $("#show").html(data);     
    });
});');

$theme->add_script($grid->command_script('#show'));

?>
<div id="show">
<?php
echo $grid->show($post->select_allposts()->fetch());
?>
</div>
<?php
$theme->render();
?>